package br.ufpe.cin.imlm2.util.filter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class SimpleCompositeFilter<T> implements CompositeFilter<T> {

	private List<IFilter<T>> filters;
	
	public SimpleCompositeFilter() {
		this.filters = new ArrayList<IFilter<T>>();
	}
	
	@Override
	public boolean retains(T t) {
		boolean retains = true;
		for (IFilter<T> f : this.filters) {
			retains = retains & f.retains(t);
		}
		return retains;
	}

	@Override
	public Collection<T> filter(Collection<T> collection) {
		Collection<T> filtered = new ArrayList<T>();
		for (T t : collection) {
			if(!this.retains(t)){
				filtered.add(t);
			}
		}
		return filtered;
	}

	@Override
	public void addFilter(IFilter<T> anotherFilter) {
		if (!this.filters.contains(anotherFilter))
			this.filters.add(anotherFilter);
	}

	@Override
	public void removeFilter(IFilter<T> anotherFilter) {
		if (this.filters.contains(anotherFilter))
			this.filters.remove(anotherFilter);
	}

}
